package com.jy.adscm.operation.purchase.controller;


import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.purchase.bean.QueryByPageParameter;
import com.jy.adscm.operation.purchase.bean.Supplier;
import com.jy.adscm.operation.purchase.service.SupplierService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 供应商基础信息表 前端控制器
 * </p>
 *
 * @author lzf
 * @since 2019-05-05
 */
@RestController
@RequestMapping("adscm/operation/supplier")
public class SupplierController {
    @Autowired
    SupplierService supplierService;
    @Autowired
    private RedisTemplate<Object, Token> tokenRedisTemplate;
    @RequestMapping("insert")
    public SimpleCoreResponse insert( @RequestBody Supplier supplier, HttpServletRequest request){
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        supplier.setSignedPerson(token.getUser().getId().toString());
        supplier.setSignedTime(new Date());
        return new SimpleCoreResponse("200","新增成功",supplierService.insert(supplier));
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody Supplier supplier){
        return new SimpleCoreResponse("200","修改成功",supplierService.updateById(supplier));
    }
    @RequestMapping("delete")
    public SimpleCoreResponse delete(Long id){
        return new SimpleCoreResponse("200","删除成功",supplierService.deleteById(id));
    }
    @RequestMapping("query")
    public SimpleCoreResponse query(){
        return new SimpleCoreResponse("200","查询成功",supplierService.queryByName());
    }

    @RequestMapping("queryByPage")
    public SimpleCoreResponse queryByCondition(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                               @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                               @RequestParam(value = "sortBy", required = false) String sortBy,
                                               @RequestParam(value = "desc",required = false)boolean desc,
                                               @RequestParam(value = "key", required = false) String key) throws AdscmSystemException {
        QueryByPageParameter queryByPageParameter=new QueryByPageParameter(currentPage,pageSize,sortBy,desc,key);
        return new SimpleCoreResponse("200","条件查询成功",supplierService.queryByCondition(queryByPageParameter));
    }
}

